package cn.zjl.testlogin.search;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.Objects;

@Data
@ToString
public class ResultOk<T> implements Serializable {

    private Integer code; // 状态码

    private String msg; // 状态码解释信息

    private Boolean flag; // 请求是否成功

    private T data; // 响应体

    public static ResultOk pass(String data){
        ResultOk<Object> resultOk = new ResultOk<>();
        resultOk.setMsg(data);
        return new ResultOk(200,resultOk.getMsg());
    }

    public ResultOk() {
    }

    public ResultOk(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultOk(Integer code, String msg, Boolean flag) {
        this.code = code;
        this.msg = msg;
        this.flag = flag;
    }

    public ResultOk(Integer code, String msg, Boolean flag, T data) {
        this.code = code;
        this.msg = msg;
        this.flag = flag;
        this.data = data;
    }

    public static <T> ResultOk<T> of(Integer code, String msg, Boolean flag) {
        return new ResultOk<>(code, msg, flag);

    }

    public static <T> ResultOk<T> of(Integer code, String msg, Boolean flag, T data) {
        return new ResultOk<>(code, msg, flag, data);

    }

    public static <T> ResultOk<T> ok(T t) {
        ResultOk<T> result = new ResultOk<>(200, "ok", true);
        if (Objects.nonNull(t)) {
            result.setData(t);
        }
        return result;
    }

    public static <T> ResultOk<T> ok() {
        return ResultOk.ok(null);
    }


    public static <T> ResultOk<T> error(T t) {
        ResultOk<T> result = new ResultOk<>(500, "error", false);
        if (Objects.nonNull(t)) {
            result.setData(t);
        }
        return result;
    }

    public static <T> ResultOk<T> error() {
        return ResultOk.error(null);
    }





}
